package com.ktgj.common.core.keyutil;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random;

/***
 * AES128加解密工具
 * @author 017918
 *
 */
public class AES128Tool {
	/**
	 * 加密
	 * @param key
	 * @param text
	 * @return
	 */
	public static String encrypt(String key, String text){
		try{
			byte []cipher = encrypt(key.getBytes(), text.getBytes());
			return new BASE64Encoder().encode(cipher);
		}catch(Exception e){
			return "";
		}
	}

	
	/**
	 * 解密
	 * @param key
	 * @param cipher
	 * @return
	 */
	public static String decrypt(String key, String cipher){
		try{
			BASE64Decoder base64de = new BASE64Decoder();
			byte []cipherBytes =  base64de.decodeBuffer(cipher);
			byte []text = decrypt(key.getBytes(), cipherBytes);
			return new String(text);
		}catch(Exception e){
			return "";
		}
	}
	
	/**
	 * 加密
	 * @param key
	 * @param text
	 * @return
	 */
	public static byte[] encrypt(byte []key, byte []text){
		try{
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			//16 bytes for AES-128
			byte[] aesKeyBytes = new byte[16];
			
			int len = key.length > 16 ? 16 : key.length;
			System.arraycopy(key, 0, aesKeyBytes, 0, len);
			
			SecretKeySpec keySpec = new SecretKeySpec(aesKeyBytes, "AES");
			
			byte[] iv = new byte[cipher.getBlockSize()];
			
			Random ran = new Random();
			//随机的初始化向量
			ran.nextBytes(iv);		
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);	
			
			byte[] results = cipher.doFinal(text);
			
			//将iv和密文拼接后返回
			byte[] retBytes = new byte[iv.length + results.length];
			System.arraycopy(iv, 0, retBytes, 0, iv.length);
			System.arraycopy(results, 0, retBytes, iv.length, results.length);
			
			return retBytes;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 解密
	 * @param key
	 * @param cipher
	 * @return
	 */
	public static byte[] decrypt(byte []key, byte []cipher){
		try{
			if (cipher.length < 16){
				return null;
			}
			
			Cipher cp = Cipher.getInstance("AES/CBC/PKCS5Padding");
			//16 bytes for AES-128
			byte[] aesKeyBytes = new byte[16];	
			
			int len = key.length > 16 ? 16 : key.length;
			System.arraycopy(key, 0, aesKeyBytes, 0, len);
			
			SecretKeySpec keySpec = new SecretKeySpec(aesKeyBytes, "AES");
			
			byte[] iv = new byte[cp.getBlockSize()];

			//将iv和密文拼接后返回
			System.arraycopy(cipher, 0, iv, 0, iv.length);
			
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cp.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
			
			byte []cip = new byte[cipher.length - iv.length];
			
			System.arraycopy(cipher, iv.length, cip, 0, cip.length);
			byte[] results = cp.doFinal(cip);
			
			return results;
		}catch(Exception e){
			return null;
		}
	}
	public static void main(String[] args) {
		/*String key = "QWtZC7zKv3EHASw5";
		String text = "13023179430";
		System.out.println(encrypt(key,text));*/
		String key = "QWtZC7zKv3EHASw5";
		String text = "U2FsdGVkX1+QbQ5HNgQzfdY4cFnP4sWkE9X/hPN7JQQ=";
		System.out.println(decrypt(key,text));
	}
}
